package com.dotools.toutiaolibrary;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.bykv.vk.openvk.TTNtExpressObject;
import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfDislike;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.VfSlot;

import java.util.List;

import api.ttfeed.Express_API_TT;

public class TT_Express extends Express_API_TT {

    /**
     * 个性化模板信息流广告，
     * 开发者不用自行对广告样式进行编辑和渲染，可直接调用相关接口获取广告view,广告样式模板以及微调后期支持开发者编辑
     * 2.5.xx版本后申请的KEY 后台强制要用渲染模版
     */
    private TTVfNative mTTAdNative;
    private TTNtExpressObject ttOb;

    @Override
    public void LoadTTExpress(final Context context, String appid, String adposid, int expressViewWidth,
                              int expressViewHeight, int count, int downloadMode, boolean isDebug,
                              final ViewGroup container, final TTExpressListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                return;
            }
        } else {
            return;
        }
        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(context.getApplicationContext());
            VfSlot adSlot = new VfSlot.Builder()
                    .setCodeId(adposid)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(640, 320)
                    .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                    .setAdCount(count)
                    .build();
            if (downloadMode == 1) {
                adSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            mTTAdNative.loadNtExpressVn(adSlot, new TTVfNative.NtExpressVfListener() {
                @Override
                public void onError(int i, String s) {
                    listener.onError(i, s);
                }

                @Override
                public void onNtExpressVnLoad(List<TTNtExpressObject> list) {
                    listener.onLoad(list.size());
                    ttOb = list.get(0);
                    bindObListener(context, ttOb, container, listener);
                }
            });
        }else{
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }

    //绑定广告行为
    private void bindObListener(Context context, TTNtExpressObject ad,
                                final ViewGroup container, final TTExpressListener listener) {
        ad.setExpressInteractionListener(new TTNtExpressObject.ExpressNtInteractionListener() {
            @Override
            public void onClicked(View view, int i) {
                listener.onObClicked(i);
            }

            @Override
            public void onShow(View view, int i) {
                listener.onObShow(i);
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                listener.onRenderFail(msg, code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                listener.onRenderSuccess();
                container.removeAllViews();
                container.addView(view);
            }
        });
        ad.render();
        //dislike设置
        bindDislike(context, ad, container);
    }

    /**
     * 设置广告的不喜欢，注意：强烈建议设置该逻辑，如果不设置dislike处理逻辑，则模板广告中的 dislike区域不响应dislike事件。
     *
     * @param ad
     */
    private void bindDislike(Context context, TTNtExpressObject ad, final ViewGroup container) {
        if (context instanceof Activity) {
            //使用默认模板中默认dislike弹出样式
            ad.setDislikeCallback((Activity) context, new TTVfDislike.DislikeInteractionCallback() {

                @Override
                public void onShow() {

                }

                @Override
                public void onSelected(int i, String s, boolean b) {
                    //用户选择不喜欢原因后，移除广告展示
                    container.removeAllViews();
                    container.setVisibility(View.GONE);
                }

                @Override
                public void onCancel() {

                }
            });
        }
    }


    @Override
    public void expressDestroy() {
        if (ttOb != null) {
            ttOb.destroy();
        }
    }
}
